package com.example.facemanager.controller;

import com.example.facemanager.config.Group;
import com.example.facemanager.service.BaiduFaceManagerService;
import com.example.facemanager.vo.RcvParam;
import com.example.facemanager.vo.ReturnMsg;
import com.sun.xml.internal.ws.util.StringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @Author huchenying
 * @Date 2022/8/3 09:47
 * @Description 一个对接百度人脸识别的DEMO
 */
@RestController
@RequestMapping("/face")
@Slf4j
public class FaceController {

    @Autowired
    BaiduFaceManagerService faceManagerService;


    /**
     * @Author huchenying
     * @Date 2022/8/3 09:51
     * @Description 上传个人信息和人脸数据
     */
    @PutMapping("/addUser")
    public ReturnMsg addUser(@RequestBody RcvParam param) {
        log.debug("addUser start param -> {}", param.toString());
        if(param.getUserName() == null || "".equals(param.getUserName())){
            return ReturnMsg.fail("用户名不能为空！");
        }
        return faceManagerService.doAddUser(param.getImg(), String.valueOf(System.currentTimeMillis()), param.getUserGroup(), param.getUserName());
    }

    /**
     * @Author huchenying
     * @Date 2022/8/3 09:55
     * @Description 检索人脸信息，成功就返回人物姓名
     */
    @PostMapping("/search")
    public ReturnMsg search(@RequestBody RcvParam param) {
        log.debug("search start param -> {}", param.toString());
        return faceManagerService.doSearch(param.getImg(), param.getUserGroup());
    }

    /**
     * @Author huchenying
     * @Date 2022/8/3 11:01
     * @Description 添加用户组
     */
    @PutMapping("/addUserGroup")
    public ReturnMsg addUserGroup(@RequestBody RcvParam param) {
        log.debug("addUserGroup start param -> {}", param.toString());
        return faceManagerService.doAddUserGroup(param.getUserGroup());
    }

    @GetMapping("/queryUserGroup")
    public ReturnMsg queryUserGroup(){
        return faceManagerService.doGetUserGroupList();
    }

    /**
     * @Author huchenying
     * @Date 2022/8/3 11:02
     * @Description 删除用户组
     */
    @DeleteMapping("/deleteUserGroup")
    public ReturnMsg deleteUserGroup(@RequestBody RcvParam param) {
        log.debug("deleteUserGroup start param -> {}", param.toString());
        return faceManagerService.doDeleteUserGroup(param.getUserGroup());
    }

    /**
     * @Author huchenying
     * @Date 2022/8/3 11:02
     * @Description 删除用户
     */
    @DeleteMapping("/deleteUser")
    public ReturnMsg deleteUser(@RequestBody RcvParam param) {
        log.debug("deleteUser start param -> {}", param.toString());
        return ReturnMsg.success();
    }

    /**
      * @Author huchenying
      * @Date   2022/8/5 09:36
      * @Description 查询所有用户
    */
    @PostMapping("/queryUser")
    public ReturnMsg queryUser(@Validated(Group.class) @RequestBody RcvParam param){
        return faceManagerService.doGetUser(param.getUserGroup());
    }
}
